package redis;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

@Slf4j
public class RedissonDemo {

    private final RedissonClient redissonClient;

    public RedissonDemo() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379").setDatabase(0);
        redissonClient = Redisson.create(config);
    }

    public static void main(String[] args) {
        RedissonDemo redissonDemo = new RedissonDemo();
        redissonDemo.get();
        redissonDemo.bloom();
    }

    private void get() {
        RBucket<Object> bucket = redissonClient.getBucket("redisson");
//        bucket.set("3.23.4");
        log.info("redisson={}", bucket.get());
    }

    private void bloom() {
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter("seqId");
        bloomFilter.tryInit(100, 0.1);
        bloomFilter.add("100");
        bloomFilter.add("200");
        bloomFilter.add("300");
        bloomFilter.expire(Duration.ofMinutes(30));
        for (int i = 0; i < 100; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.info("===> {}, {}", bloomFilter.contains("200"), bloomFilter.contains("400"));
        }

    }
}
